﻿using System.Collections.Generic;
using MTC.Web.Business.DTO;
using Vehicle = MTC.Business.Vehicle.Vehicle;

namespace MTC.Web.Business
{
    public class Vehicles
    {
        private List<VehicleEntity> _vehicleEntityList = new List<VehicleEntity>();

        public List<VehicleEntity> GetVehicles(int vehicleid)
        {
            List<MTC.Common.DTO.Vehicle> vehicles;
            vehicles = MTC.Business.Vehicle.Vehicle.GetVehicles();
            if (vehicles == null)
                return null;
            _vehicleEntityList.Clear();
            foreach (MTC.Common.DTO.Vehicle vehicle in vehicles)
            {
                VehicleEntity entity = new VehicleEntity();
                entity.VehicleId = vehicle.VehicleId;
                entity.BodyType = vehicle.BodyType;
                entity.CarExpiryDate = vehicle.CarExpiryDate;
                entity.EngineNo = vehicle.EngineNo;
                entity.InspectionDate = vehicle.InspectionDate;
                entity.ManufactureDate = vehicle.ManufactureDate;
                entity.Model = vehicle.Model;
                entity.OwnerId = vehicle.OwnerId;
                entity.PurchaseDate = vehicle.PurchaseDate;
                entity.PurchasePrice = vehicle.PurchasePrice;
                entity.RegistrationDueDate = vehicle.RegistrationDueDate;
                entity.Rego = vehicle.Rego;
                entity.BodyTypeDescription = vehicle.BodyTypeDescription;
                _vehicleEntityList.Add(entity);
            }
            return _vehicleEntityList;
        }

        public VehicleEntity GetAVehicle(int vehicleid )
        {
            if (vehicleid == 0)
                return null;
            VehicleEntity entity = null;
            MTC.Common.DTO.Vehicle vehicle = MTC.Business.Vehicle.Vehicle.GetVehicle(vehicleid);
            if (vehicleid != 0)
            {
                entity = new VehicleEntity();
                entity.VehicleId = vehicle.VehicleId;
                entity.BodyType = vehicle.BodyType;
                entity.CarExpiryDate = vehicle.CarExpiryDate;
                entity.EngineNo = vehicle.EngineNo;
                entity.InspectionDate = vehicle.InspectionDate;
                entity.ManufactureDate = vehicle.ManufactureDate;
                entity.Model = vehicle.Model;
                entity.OwnerId = vehicle.OwnerId;
                entity.PurchaseDate = vehicle.PurchaseDate;
                entity.PurchasePrice = vehicle.PurchasePrice;
                entity.RegistrationDueDate = vehicle.RegistrationDueDate;
                entity.Rego = vehicle.Rego;
            }
            return entity;
        }

        public bool UpdateAVehicle(VehicleEntity entity)
        {
            if (entity==null)
            {
                return false;
            }
            MTC.Common.DTO.Vehicle vehicle = new Common.DTO.Vehicle();

            vehicle.VehicleId = entity.VehicleId;
            vehicle.BodyType = entity.BodyType;
            vehicle.CarExpiryDate = entity.CarExpiryDate;
            vehicle.EngineNo = entity.EngineNo;
            vehicle.InspectionDate = entity.InspectionDate;
            vehicle.ManufactureDate = entity.ManufactureDate;
            vehicle.Model = entity.Model;
            vehicle.OwnerId = entity.OwnerId;
            vehicle.PurchaseDate = entity.PurchaseDate;
            vehicle.PurchasePrice = entity.PurchasePrice;
            vehicle.RegistrationDueDate = entity.RegistrationDueDate;
            vehicle.Rego = entity.Rego;

            return MTC.Business.Vehicle.Vehicle.UpdateVehicle(vehicle);


        }

        public bool DeleteAVehicle(int VehicleId)
        {
            if (VehicleId == 0)
                return false;

            return MTC.Business.Vehicle.Vehicle.DeleteVehicle(VehicleId);
        }

        public int AddVehicle(VehicleEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            MTC.Common.DTO.Vehicle vehicle = new Common.DTO.Vehicle();

            vehicle.VehicleId = entity.VehicleId;
            vehicle.BodyType = entity.BodyType;
            vehicle.CarExpiryDate = entity.CarExpiryDate;
            vehicle.EngineNo = entity.EngineNo;
            vehicle.InspectionDate = entity.InspectionDate;
            vehicle.ManufactureDate = entity.ManufactureDate;
            vehicle.Model = entity.Model;
            vehicle.OwnerId = entity.OwnerId;
            vehicle.PurchaseDate = entity.PurchaseDate;
            vehicle.PurchasePrice = entity.PurchasePrice;
            vehicle.RegistrationDueDate = entity.RegistrationDueDate;
            vehicle.Rego = entity.Rego;

            return MTC.Business.Vehicle.Vehicle.AddVehicle(vehicle);
            
        }
    }
}
